<!--
  * 佣金确认列表
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-col :span="5">
        <a-form-item label="分润月份" class="smart-query-form-item">
          <a-range-picker :allowClear="false" :inputReadOnly="true" v-model:value="queryForm.monthTime" picker="month" @change="onChangeTime" />
          <!-- <a-date-picker :allowClear="false" :inputReadOnly="true" picker="month" v-model:value="queryForm.fTime" /> -->
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="状态" class="smart-query-form-item">
          <a-select v-model:value="queryForm.confirmedFlag" allowClear :options="CONFIRMED_STATUS" placeholder="请选择状态" />
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-form-item label="代理商名称" class="smart-query-form-item">
          <a-input v-model:value="queryForm.agentNameLike" placeholder="输入代理商名称" />
        </a-form-item>
      </a-col>
      <!-- <a-col :span="3">
        <a-form-item label="处理结果" class="smart-query-form-item">
          <a-select v-model:value="queryForm.businessType" allowClear :options="CHANNEL_BUSINESS_TYPE" placeholder="请选择处理结果" />
        </a-form-item>
      </a-col> -->
      <!-- <a-col :span="3">
        <a-form-item label="签约日期" class="smart-query-form-item">
          <a-date-picker :allowClear="false" :inputReadOnly="true" v-model:value="queryForm.fTime" />
        </a-form-item>
      </a-col> -->
      <!-- 
      <a-form-item label="状态" class="smart-query-form-item">
        <SmartEnumSelect width="200px" v-model:value="queryForm.status" enum-name="STATUS_ENUM" placeholder="状态" />
      </a-form-item> 
      -->
      <a-col :span="5">
        <a-form-item class="smart-query-form-item">
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-form-item>
      </a-col>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-title">数据列表</div>
      <!-- <div class="smart-table-operate-block">
      </div> -->
      <div class="smart-table-setting-block">
        <!-- <a-button @click="showForm" type="primary" size="small" v-privilege="'trade:insert'">
          <template #icon>
            <PlusOutlined />
          </template>
          添加
        </a-button> -->
        <!-- <TableOperator v-model="columns" :tableId="null" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <!-- <template #bodyCell="{ text, record, column }"> -->
      <!--  v-privilege="'trade:update'" -->
      <!-- <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="view(record)" type="link">查看合同</a-button>
          </div>
        </template> -->
      <!-- </template> -->
    </a-table>

    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :page_sizeOptions="PAGE_SIZE_OPTIONS"
        :defaultpage_size="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
</template>
<script setup>
  import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
  import { message, Modal } from 'ant-design-vue'
  import { profitReportApi } from '/@/api/business/b-operation/profit/profit-report'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import CustomSelect from '/@/components/business/comment/CustomSelect.vue'
  import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue'
  import dayjs from 'dayjs'
  // import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue'
  import { selectDictLabel, formatToMoney, strToArr } from '/@/utils/common'
  import {
    PRODUCT_TYPE, //产品类型
    CONFIRMED_STATUS, //确认状态
  } from '/@/constants/dict'
  import { cloneDeep } from 'lodash-es'
  import { myUpload } from '/@/utils/myUpload'

  const { proxy } = getCurrentInstance()

  // ---------------------------- 表格列 ----------------------------
  const columns = ref([
    {
      title: '序号',
      dataIndex: 'index',
      width: 60,
      customRender: ({ index }) => {
        return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
      },
    },
    {
      title: '分润月份',
      dataIndex: 'monthTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM'),
      ellipsis: true,
    },
    {
      title: '代理商名称',
      dataIndex: 'agentName',
      ellipsis: true,
    },
    {
      title: '数据源',
      dataIndex: 'dataSource',
      ellipsis: true,
    },
    {
      title: '支付通道',
      dataIndex: 'channelName',
      ellipsis: true,
    },
    {
      title: '交易类型',
      dataIndex: 'payType',
      customRender: ({ text }) => {
        return proxy.$smartEnumPlugin.getDescByValue('ZZB_PAY_TYPE_ENUM', text)
      },
      ellipsis: true,
    },
    {
      title: '交易金额',
      dataIndex: 'tradeAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '交易笔数',
      dataIndex: 'tradeNum',
      ellipsis: true,
    },
    {
      title: '退款金额',
      dataIndex: 'refundAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '退款笔数',
      dataIndex: 'refundNum',
      ellipsis: true,
    },
    {
      title: '营收金额',
      dataIndex: 'receivedAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '结算金额',
      dataIndex: 'settleAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '营收笔数',
      dataIndex: 'receivedNum',
      ellipsis: true,
    },
    {
      title: '分润金额',
      dataIndex: 'profitAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '状态',
      dataIndex: 'confirmedFlag',
      customRender: (column) => (column.text ? '已确认' : '待确认'),
      ellipsis: true,
    },
    // {
    //   title: '操作',
    //   dataIndex: 'action',
    //   fixed: 'right',
    //   width: 200,
    // },
  ])

  const onChangeTime = (data) => {
    queryForm.monthTimeBegin = data[0].startOf('days').valueOf()
    queryForm.monthTimeEnd = data[1].endOf('days').valueOf()
  }
  // ---------------------------- 查询数据表单和方法 ----------------------------
  const queryFormState = {
    confirmedFlag: undefined, //通道类型
    agentNameLike: undefined, //产品类型
    monthTime: [dayjs().startOf('month'), dayjs().endOf('month')], //
    monthTimeBegin: dayjs().startOf('month').valueOf(), //
    monthTimeEnd: dayjs().endOf('month').valueOf(), //
    pageNo: 1,
    pageSize: 10,
  }

  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await profitReportApi.page(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }
  onMounted(queryData)

  // 查看合同
  function view(row) {
    // openDetail.value = true
  }
</script>
